package com.limit.control.admin;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.ClearInterceptor;
import com.jfinal.plugin.ehcache.CacheKit;
import com.limit.constant.Constant;
import com.limit.utils.DateUtil;

public class IndexController extends BaseController{

	public void index(){
		
		this.render("index.html");
	}
	
	public void left(){
		List<Map<String,Object>> upl = CacheKit.get(Constant.CacheActorPermission, String.valueOf(this.getSessionAttr(Constant.UserName)));
			
		List<Map<String,Object>> mlist = new ArrayList<Map<String,Object>>();
		
		if(null!=upl && !upl.isEmpty()){
			List<Map<String,Object>> plist = new ArrayList<Map<String,Object>>();
			for(Map<String,Object> map : upl){
				int pid = Integer.valueOf(String.valueOf(map.get("pid")));
				String menuName = String.valueOf(map.get("name"));
				if(0==pid && !"null".equals(menuName) && !"".equals(menuName)){
					plist.add(map);
				}
			}
			System.out.println(plist);
			for(Map<String,Object> map : plist){
				Map<String,Object> menu = new HashMap<String,Object>();
				List<Map<String,Object>> ml = new ArrayList<Map<String,Object>>();
				int id = Integer.valueOf(String.valueOf(map.get("id")));
				String menuTitle = String.valueOf(map.get("name"));
				for(Map<String,Object> m : upl){
					int pid = Integer.valueOf(String.valueOf(m.get("pid")));
					String menuName = String.valueOf(m.get("name"));
					if(id==pid && !"null".equals(menuName) && !"".equals(menuName)){
						ml.add(m);
					}
				}
				menu.put("menuTitle", menuTitle);
				menu.put("subMenus", ml);
				mlist.add(menu);
			}
		}
//		System.out.println("**************");
//		System.out.println(mlist);
		this.setAttr("menuList", mlist);
		this.render("left.html");
	}
	
	public void top() throws ParseException{
		this.setAttr("username", String.valueOf(this.getSessionAttr(Constant.UserName)));
		this.setAttr("now", DateUtil.getTime(System.currentTimeMillis(), 2));
		this.render("top.html");
	}
	
	public void down(){
		this.render("down.html");
	}
	
	public void main(){
		this.render("main.html");
	}
	
	@ClearInterceptor
	public void nopermission(){
		this.render("nopermission.html");
	}
}
